import { defineStore } from "pinia";
import {computed, Ref, ref} from "vue";
import {ErnieRequestMessage} from "../chatGptUtils";
export type User = {
    id: number,
    name: string,
    avatar: string,
    gender: boolean,
    intro: string,
    userType: UserType,
}

export enum UserType {
    UserRoom,
    GroupRoom
}

export enum MessageType {
    TEXT,
    IMAGE
}

export enum SessionType {
    PRIVATE,
    GROUP,
    GPT
}

export type Message = {
    messageId: number,
    fromId: number,
    toId: number,
    text: string,
    msgType: MessageType,
    sessionType: SessionType,
    msgDate: Date
}

type CurrentDate = {
    year: number,
    month: number,
    date: number,
    day: number,
    hours: number,
    minutes: number
}

const mySelf: User = {
    id: 292927,
    name: 'Melody',
    avatar: 'https://mini-avartar.oss-cn-chengdu.aliyuncs.com/292927.jpg',
    gender: false,
    intro: "Don't worry, everything will be okay in the end. If it's not okay, then it's not the end.",
    userType: UserType.UserRoom
}

//const mySelf: User = {id: 78886, name: 'tiny', avatar: 'src/assets/img/02.jpg', gender: true, intro: "it's time"}


const msgList = new Array<Message>();
msgList.push({messageId: 24565, fromId: 292927, toId: 78886, msgType: MessageType.TEXT, sessionType: SessionType.PRIVATE, msgDate: new Date(), text: "m send to t"});
msgList.push({messageId: 24565, fromId: 292927, toId: 78886, msgType: MessageType.TEXT, sessionType: SessionType.PRIVATE, msgDate: new Date(), text: "m send to t 2"});
msgList.push({messageId: 24565, fromId: 78886, toId: 292927, msgType: MessageType.TEXT, sessionType: SessionType.PRIVATE, msgDate: new Date(), text: "t send to m "});
msgList.push({messageId: 24565, fromId: 78886, toId: 292927, msgType: MessageType.TEXT, sessionType: SessionType.PRIVATE, msgDate: new Date(), text: "t send to m 2"});
msgList.push({messageId: 435455, fromId: 14893, toId: 78886, msgType: MessageType.TEXT, sessionType: SessionType.PRIVATE, msgDate: new Date(), text: "ok, i see"});


const list = new Array<User>()
list.push({id: 121212, name: 'Ernie Bot', avatar: 'src/assets/img/gptAvarta.jpg', gender: false, intro: 'this is gpt', userType: UserType.UserRoom})
list.push({id: 98585, name: 'coco', avatar: 'src/assets/img/01.jpg', gender: true, intro: '', userType: UserType.UserRoom})
list.push({id: 78886, name: 'tiny', avatar: 'src/assets/img/02.jpg', gender: true, intro: '', userType: UserType.UserRoom})
// list.push({
//     id: 292927,
//     name: 'Melody',
//     avatar: 'src/assets/img/avatar.jpg',
//     gender: false,
//     intro: "Don't worry, everything will be okay in the end. If it's not okay, then it's not the end."
// })
list.push({id: 35465, name: 'kali', avatar: 'src/assets/img/03.jpg', gender: false, intro: '', userType: UserType.UserRoom})
list.push({id: 78465, name: 'testament', avatar: 'src/assets/img/04.jpg', gender: false, intro: '', userType: UserType.UserRoom})
list.push({id: 14893, name: 'aly', avatar: 'src/assets/img/05.jpg', gender: false, intro: '', userType: UserType.UserRoom})


const msgMap = new Map<number, Array<Message>>();
for (const u of list)
    msgMap.set(u.id, new Array<Message>());
for (const msg of msgList) {
    if (msg.toId === mySelf.id)
        msgMap.get(msg.fromId)?.push(msg)
    else
        msgMap.get(msg.toId)?.push(msg)
}


const userMapperOrigin: Map<number, User> = new Map<number, User>();
list.forEach(v => {
    userMapperOrigin.set(v.id, v)
})


const ernieHistoryMesssage: Array<ErnieRequestMessage> = new Array<ErnieRequestMessage>();


export const messageStore = defineStore('messageStore', () => {
    const userList = ref(list)

    const timeId: Ref<any> = ref(null)

    const getUserList = () => userList.value
    const date: Ref<Date> = ref(new Date())
    const setDate = () => date.value = new Date()

    const currentUserId: Ref<number> = ref(0)
    const getCurrentUserId = computed(() => currentUserId.value)
    const setCurrentUserId = (id: number) => currentUserId.value = id

    const userMapper: Ref<Map<number, User>> = ref(userMapperOrigin)
    const getUserMapper = () => userMapper.value
    const userInfo: Ref<User> = ref(mySelf)
    const getUserInfo = () => userInfo.value

    const msgMapRef = ref(msgMap)
    const getMsgMap = () => msgMapRef.value

    const ernieHistoryMessageRef = ref(ernieHistoryMesssage)
    const getErnieHistoryMessage = () => ernieHistoryMessageRef.value

    const getDate = computed((): CurrentDate => {

        return {
            year: date.value.getFullYear(),
            month: date.value.getMonth(),
            date: date.value.getDate(),
            day: date.value.getDay(),
            hours: date.value.getHours(),
            minutes: date.value.getMinutes()
        }
    })

    return {
        getUserList,
        getUserInfo,
        getUserMapper,
        getCurrentUserId,
        setCurrentUserId,
        getDate,
        timeId,
        setDate,
        getMsgMap,
        getErnieHistoryMessage,
    }
})
